package com.hzw.saas.service.hpc.listener.handler;

import cn.hutool.core.util.StrUtil;
import com.hzw.saas.api.hpc.IHpcAccountProviderService;
import com.hzw.saas.api.hpc.IHpcAccountRecordService;
import com.hzw.saas.api.hpc.bo.HpcAccountBo;
import com.hzw.saas.api.hpc.bo.HpcAccountProviderBo;
import com.hzw.saas.api.hpc.bo.HpcAccountRecordBo;
import com.hzw.saas.api.hpc.enums.HpcProviderType;
import com.hzw.saas.common.config.exception.DefaultSaasException;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.service.hpc.service.IHpcAccountSlurmService;
import com.hzw.saas.service.hpc.service.IQingCloudService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 山河HPC账号处理器
 *
 * @author sonam
 * @sine 2021/9/8 3:44 下午
 */
@Slf4j
@Service("qc")
public class QCHpcAccountHandlerImpl extends AbstractHpcAccountHandler {

    @Resource
    private IHpcAccountRecordService recordService;
    @Resource
    private IHpcAccountSlurmService slurmService;
    @Resource
    private IQingCloudService qingCloudService;

    public QCHpcAccountHandlerImpl(IHpcAccountProviderService providerService) {
        super(providerService);
    }

    @Override
    protected void doHpcAccountAddHandle(HpcAccountProviderBo providerBo, HpcAccountBo hpcAccount) {
        try {
            qingCloudService.addUser(providerBo, hpcAccount.getHpcAccount(), hpcAccount.getHpcPassword());
        } catch (Exception e) {
            log.error("request error: {}", e.getMessage());
            // 检查是否包含已创建的用户
            AssertUtil.assertThrow("集群与系统信息不同步，该账号已存在，请尝试更换HPC用户名", qingCloudService.getUsers(providerBo).contains(hpcAccount.getHpcAccount()));
            throw DefaultSaasException.build(e.getMessage(), e);
        }
    }

    @Override
    protected void doHpcAccountDeleteHandle(HpcAccountProviderBo providerBo, HpcAccountBo hpcAccountBo) {
        try {
            // 删除slurm用户
            slurmService.deleteSlurmUser(providerBo, hpcAccountBo.getHpcAccount(), StrUtil.EMPTY);
            // 发请求删除用户
            qingCloudService.deleteUser(providerBo, hpcAccountBo.getHpcAccount());
        } catch (Exception e) {
            log.error("request error: {}", e.getMessage());
            throw DefaultSaasException.build(e.getMessage(), e);
        }
    }

    @Override
    protected void doHpcAccountAllotHandle(HpcAccountProviderBo providerBo, HpcAccountBo hpcAccountBo) {
        String hpcAccountId = hpcAccountBo.getHpcAccountId();
        HpcAccountRecordBo hpcAccountRecordBo = recordService.searchOneByHpcAccountId(hpcAccountId);
        AssertUtil.assertThrow("内部业务错误", Objects.isNull(hpcAccountRecordBo));
        String slurmAccount = hpcAccountRecordBo.getSlurmAccount();
        AssertUtil.assertThrow("参数错误，没有指定slurm用户", StrUtil.isBlank(slurmAccount));
        slurmService.addSlurmUser(providerBo, hpcAccountBo.getHpcAccount(), slurmAccount);
    }

    @Override
    protected void doHpcAccountUnAllotHandle(HpcAccountProviderBo providerBo, HpcAccountBo hpcAccountBo) {
        slurmService.deleteSlurmUser(providerBo, hpcAccountBo.getHpcAccount(), StrUtil.EMPTY);
    }

    @Override
    protected String getProviderCode() {
        return HpcProviderType.QY.value();
    }

}
